<template>
    <el-dialog
        :modelValue="dialogVisible"
        @update:modelValue="(e)=>{emit('update:dialogVisible',e)}"
        title="Tips"
        width="50%"
        @open="handleOpen"
        :close-on-click-modal="false"
    >
        <el-form :model="formParams">
            <el-form-item label="name" :label-width="formLabelWidth">
                <el-input v-model="formParams.name" autocomplete="off" />
            </el-form-item>
            <template v-if="type === 'add'">
                <el-form-item label="adminEmailAddress" :label-width="formLabelWidth">
                    <el-input v-model="formParams.adminEmailAddress" autocomplete="off" />
                </el-form-item>
                <el-form-item label="adminPassword" :label-width="formLabelWidth">
                    <el-input v-model="formParams.adminPassword" autocomplete="off" />
                </el-form-item>
            </template>

        </el-form>


        <template #footer>
            <span class="dialog-footer">
                <el-button @click="emit('update:dialogVisible',false)">Cancel</el-button>
                <el-button type="primary" @click="sureClick">
                    Confirm
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { reactive, ref, watch } from 'vue'
import {tenantsCreate,tenantsUpdate} from '@/api/tenant'
import { ElMessage, ElMessageBox } from 'element-plus'

const props = defineProps({
  dialogVisible: {
    type:Boolean,
    default:false
  },
  type:{
    type:String,
    default:'add'
  },
  editObject:{
    type:Object,
    default:function() {
      return null
    }
  }
})
const emit = defineEmits(['update:dialogVisible','hanldeRequest']);
const formLabelWidth = '140px'
const formParams = ref({
  name:'',
  adminEmailAddress:'',
  adminPassword:''
})

function handleOpen() {
  if(props.type === 'add') {
    formParams.value = {
      name:'',
      adminEmailAddress:'',
      adminPassword:''
    }
  }else if(props.type === 'edit') {
    console.log('props.editObject',props.editObject)
    formParams.value.name = props.editObject.name;
  }
}

async function sureClick() {
  if(props.type === 'add') {
    await tenantsCreate({...formParams.value})
    ElMessage({
      type: 'success',
      message: '新增成功',
    })
    emit('update:dialogVisible',false)
    emit('hanldeRequest')
  }else if(props.type === 'edit') {
    await tenantsUpdate({name:formParams.value.name,id:props.editObject.id})
    ElMessage({
      type: 'success',
      message: '修改成功',
    })
    emit('update:dialogVisible',false)
    emit('hanldeRequest')
  }
}
</script>

<style lang="scss" scoped>

</style>